package com.example.wishstar.view.adpter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wishstar.R;
import com.example.wishstar.databinding.MemoryItemBinding;
import com.example.wishstar.model.Memory;
import com.example.wishstar.view.CustomOperateDialog;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;

import java.util.List;

public class MemoryAdapter extends RecyclerView.Adapter<MemoryAdapter.ViewHolder> {

    private String TAG = "MemoryAdapter";
    private List<Memory> mMemoryList;
    private Context mcontext;
    private OnMemoryItemClickListener onMemoryItemClickListener;

    public interface OnMemoryItemClickListener {
        void onItemClick(Memory memory);
    }

    public void setOnMemoryItemClickListener(OnMemoryItemClickListener onMemoryItemClickListener) {
        this.onMemoryItemClickListener = onMemoryItemClickListener;
    }

    public MemoryAdapter(List<Memory> mMemoryList, Context mcontext) {
        this.mMemoryList = mMemoryList;
        this.mcontext = mcontext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MemoryItemBinding memoryItemBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()), R.layout.memory_item, parent, false);

        return new ViewHolder(memoryItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Memory memory = mMemoryList.get(position);
        holder.memoryItemBinding.setMemory(memory);
        if (memory.type == 1) {//倒计时
            holder.memoryItemBinding.memoryNumText.setBackgroundColor(mcontext.getColor(R.color.blue_light));
            holder.txtMemoryTianDrawableBackground.setColor(mcontext.getColor(R.color.blue_dark));
        }
        else {
            holder.memoryItemBinding.memoryNumText.setBackgroundColor(mcontext.getColor(R.color.yellow_light));
            holder.txtMemoryTianDrawableBackground.setColor(mcontext.getColor(R.color.yellow_dark));
        }

        holder.memoryItemBinding.memoryItemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                initPopWindow(v, pos);
                return false;
            }
        });
    }

    private void initPopWindow(View v, int pos) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.memory_item_delete, null, false);
        TextView textView  = view.findViewById(R.id.btn_delete);
        //1.构造一个PopupWindow，参数依次是加载的View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效


        //设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(v, 40, -60);

        //设置popupWindow里的按钮的事件
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onMemoryItemClickListener.onItemClick(mMemoryList.get(pos));
                Toast.makeText(mcontext, "你点击了删除", Toast.LENGTH_SHORT).show();
                popWindow.dismiss();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMemoryList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        MemoryItemBinding memoryItemBinding;
        GradientDrawable txtMemoryTianDrawableBackground;

        public ViewHolder(@NonNull MemoryItemBinding itemView) {
            super(itemView.getRoot());
            memoryItemBinding = itemView;
            txtMemoryTianDrawableBackground = (GradientDrawable) memoryItemBinding.memoryTianText.getBackground();
        }
    }
}
